package zhuyun.code.datamanager.viewmodel

import android.app.Activity
import android.app.Application
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import zhuyun.code.datamanager.model.MysqlDatabase

/**
 * @author: Administrator
 * @date: 2021/11/2
 */
class ViewModelManager<T: ViewModel> {
    lateinit var activity:Activity

    fun getViewModel(activity :Activity,modelClass: Class<T>):T{
        this.activity = activity
        return ViewModelProviders.of(activity as FragmentActivity,
            AccountViewModelFactory(activity.application)
        ).get(modelClass)
    }



    private inner class AccountViewModelFactory(application: Application) : ViewModelProvider.AndroidViewModelFactory(application) {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            if (modelClass == AccountViewModel::class.java) {
                return AccountViewModel(MysqlDatabase.getInstance()) as T
            }
            return super.create(modelClass)
        }
    }

}